/**
 * Project Name:inonepay-api
 * File Name:PayController.java
 * Package Name:com.inone.inonepay.api.controller
 * Date:2016��6��27������7:09:33
 * Copyright (c) 2016, www.zbj.com All Rights Reserved.
 *
*/

package com.inone.inonepay.api.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * ClassName:PayController <br/>
 * Function: TODO ��������. <br/>
 * Date: 2016��6��27�� ����7:09:33 <br/>
 * 
 * @author ��Զ��
 * @version
 * @since JDK 1.7
 * @see
 */
@RestController
public class PayController {
	@Autowired
	RestTemplate restTemplate;
	@Value(value = "${mysqldb.datasource.url:hello world}")
	public String myData;

	public String SERVICE_NAME = "INONEPAY-ORDER-SERVICE";
	@Autowired
    private DiscoveryClient discoveryClient;
	
    /**
     * LoadBalanced 注解表明restTemplate使用LoadBalancerClient执行请求
     *
     * @return
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        RestTemplate template = new RestTemplate();
        SimpleClientHttpRequestFactory factory = (SimpleClientHttpRequestFactory) template.getRequestFactory();
        factory.setConnectTimeout(3000);
        factory.setReadTimeout(3000);
        return template;
    }

	@ResponseBody
	@RequestMapping(method = RequestMethod.GET, value = "pay")
	public String pay() {
		return "zdy" + myData;
	}
	
	@ResponseBody
	@RequestMapping(method = RequestMethod.GET, value = "queryorder")
	public List<String> queryorder()
	{
		 return restTemplate.getForObject("http://"+SERVICE_NAME+"/orderquery", List.class);
	}
	
	  
	    @RequestMapping("/discovery")
	    public String doDiscoveryService(){
	        StringBuilder buf = new StringBuilder();
	        List<String> serviceIds = discoveryClient.getServices();
	        if(!CollectionUtils.isEmpty(serviceIds)){
	            for(String s : serviceIds){
	                System.out.println("serviceId:" + s);
	                List<ServiceInstance> serviceInstances =  discoveryClient.getInstances(s);
	                if(!CollectionUtils.isEmpty(serviceInstances)){
	                    for(ServiceInstance si:serviceInstances){
	                        buf.append("["+si.getServiceId() +" host=" +si.getHost()+" port="+si.getPort()+" uri="+si.getUri()+"]");
	                    }
	                }else{
	                    buf.append("no service.");
	                }
	            }
	        }


	        return buf.toString();
	    }
}
